package com.example.tellwin.home.act;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseActivty;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.common.Common;
import com.example.tellwin.home.adpater.CommentAdapter;
import com.example.tellwin.home.bean.CommentBean;
import com.example.tellwin.home.bean.TeactherTeamBean;
import com.example.tellwin.home.contract.TeacherDetailContract;
import com.example.tellwin.home.presenter.TeacherDetailPresenter;
import com.example.tellwin.mine.bean.TeacherBean;
import com.example.tellwin.mine.bean.TeacherContentBean;
import com.example.tellwin.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TeacherDetailActivity extends CpBaseActivty implements TeacherDetailContract.View {

    @BindView(R.id.teacher_head_iv)
    ImageView teacherHeadIv;
    @BindView(R.id.answer_number_tv)
    TextView answerNumberTv;
    @BindView(R.id.comment_number_tv)
    TextView commentNumberTv;
    @BindView(R.id.answer_time_tv)
    TextView answerTimeTv;
    @BindView(R.id.attention_tv)
    TextView attentionTv;
    @BindView(R.id.individual_resume_tv)
    TextView individualResumeTv;
    @BindView(R.id.comment_rv)
    RecyclerView commentRv;

    private CommentAdapter commentAdapter;
    private List<CommentBean> commentBeans = new ArrayList<>();

    @Inject
    TeacherDetailPresenter mPresenter;

    TeacherContentBean teacherContentBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_detail);
        ButterKnife.bind(this);
        initViews();
        initEvents();
        initDatas();
    }

    @Override
    protected void initViews() {
        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        commentRv.setLayoutManager(linearLayoutManager);
        commentAdapter = new CommentAdapter();
        commentRv.setAdapter(commentAdapter);
    }

    @Override
    protected void initEvents() {
        attentionTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.attention(teacherContentBean.getFacultyTeamDetails().getUserId());
            }
        });
    }

    @Override
    protected void initDatas() {
        TeactherTeamBean teactherTeamBean = (TeactherTeamBean) getIntent().getSerializableExtra(Common.ANSWER);
        String teacherId = getIntent().getStringExtra(Common.TEACHER_ID);
        String teacherName = getIntent().getStringExtra(Common.TEACHER_NAME);
        setTitle(teacherName);
        mPresenter.getTeacherDetail(teacherId);
    }

    @Override
    public void teacherDetailResult(TeacherContentBean teacherContentBean) {
        if (teacherContentBean == null || teacherContentBean.getFacultyTeamDetails() == null) {
            finish();
            return;
        }
        this.teacherContentBean = teacherContentBean;
        if (teacherContentBean.getFacultyTeamDetails() != null) {
            TeacherBean teacherBean = teacherContentBean.getFacultyTeamDetails();
            GlideUtils.loadHeadImage(this, teacherBean.getHeadPic(), teacherHeadIv);
            answerNumberTv.setText(teacherBean.getAnswerCount() == null ? "0" : teacherBean.getAnswerCount());
            commentNumberTv.setText(teacherBean.getEvaluateCount() + "");
            answerTimeTv.setText(teacherBean.getAnswerDuration() == null ? "0" : teacherBean.getAnswerDuration());
            individualResumeTv.setText(teacherBean.getDescribe());
            if(teacherBean.getIsFollow() ==0){
                attentionTv.setText(R.string.attention);
                attentionTv.setSelected(false);
            }else {
                attentionTv.setText(R.string.cancel);
                attentionTv.setSelected(true);
            }
        }
        if (teacherContentBean.getUserEvaluateList() != null) {
            commentAdapter.setEvalutateBeans(teacherContentBean.getUserEvaluateList());
        }
    }

    @Override
    public void attention() {
        mPresenter.getTeacherDetail(teacherContentBean.getFacultyTeamDetails().getUserId());
    }
}
